package com.gogowan.agroenterprise.newview;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gogowan.agroenterprise.LoginActivity;
import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.db.DBHelper;
import com.gogowan.agroenterprise.model.CollectCode;
import com.gogowan.agroenterprise.model.CollectCodeRequest;
import com.gogowan.agroenterprise.model.CollectData;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.model.ResultInfo;
import com.gogowan.agroenterprise.utils.Utils;
import com.gogowan.agroenterprise.widget.adapter.NewCollectHistoryAdapater;

import java.util.ArrayList;
import java.util.List;

import u.aly.V;

public class NewCollectHistory extends BaseActivity {
    private DBHelper dbHelper;
    private List<CollectCode> strings=new ArrayList<>();
    private ListView listView;
    private NewCollectHistoryAdapater adapater=null;
    
    private ProgressDialog progressDialog;
    private boolean isCheckAll=true;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_newview_history);
        initView();
    }
    private void initView() {
        dbHelper = new DBHelper(this);
        dbHelper.openDatabase();
        initData();
        initTitle("离线缓存",true);
        listView=(ListView)findViewById(R.id.newview_HistroyList);
        tv=(TextView) findViewById(R.id.selectall_tv);

        adapater=new NewCollectHistoryAdapater(this,strings);
        adapater.setListener(listener);
        listView.setAdapter(adapater);
    }
    private void initTitle(String TitleName,boolean isFinish){
        TextView titleName=(TextView)findViewById(R.id.all_top_title);
        ImageButton top=(ImageButton)findViewById(R.id.all_top_back_btn);
        titleName.setText(TitleName);
        if (isFinish){
            top.setVisibility(View.VISIBLE);
        }else {
            top.setVisibility(View.GONE);
        }
        top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void initData(){
        strings=dbHelper.getHistroyList();
        SharedPreferences preferences = getSharedPreferences("LoginINfo", MODE_PRIVATE);
        
        
    }
    public void SelectAll(View  view){
        if (strings.size()<1){
            return;
        }
        adapater.AllCheck(!isCheckAll);
        ModifyTopRightButton();
    }
    private NewCollectHistoryAdapater.onItemCheckBoxClickListener listener=new NewCollectHistoryAdapater.onItemCheckBoxClickListener() {
        @Override
        public void onClick() {
            ModifyTopRightButton();
        }
    };
    public void ModifyTopRightButton(){
        if (adapater.isCheckItem().size()==strings.size()){
            tv.setText("取消全选");
            isCheckAll=false;
        }else {
            tv.setText("全选");
            isCheckAll=true;
        }
    }
    public void SendMessage(View view){
        if (adapater.isCheckItem().size()<1){
            showToast("请选择您要上传的数据");
            return;
        }
        for (CollectCode collectCode:adapater.isCheckItem()){
            List<CollectData> l=dbHelper.getMaterialBase(collectCode.getCollectTime(),collectCode.getCollectUserName());
            collectCode.setCode(l);
            collectCode.setCollectUserId(l.get(0).getCollectUserId());
        }
        CollectCodeRequest codeRequest = new CollectCodeRequest();
//        codeRequest.setEnterpriseId(String.valueOf(loginResultInfo.getEnterpriseinfo().getEnterpriseid()));
        codeRequest.setCodeInfo(adapater.isCheckItem());
        HistroyAsytask asytask=new HistroyAsytask();
        progressDialog = ProgressDialog.show(this, getString(R.string.alert_title), "正在提交信息,请稍候....", true, false);
        asytask.execute(codeRequest);
    }
    // 数据采集上传
    class HistroyAsytask extends MyAsyncTask {

        @Override
        protected Object doInBackground(Object... params) {
            CollectCodeRequest collectCodeRequest = (CollectCodeRequest) params[0];
            String json = Utils.toJSON(collectCodeRequest);
            com.gogowan.agroenterprise.model.Message message = new com.gogowan.agroenterprise.model.Message(32000, true,
                    json);
            String res = Utils.postCommit(getString(R.string.first_url) + getString(R.string.first_url_page), message);
            return res;
        }

        @Override
        protected void onPostExecute(Object res) {
            if (isFinishing()) {
                return;
            }
            progressDialog.cancel();
            try {
                if (res == null || res.equals("")) {
                    showToast(getString(R.string.server_error));
                } else {
                    com.gogowan.agroenterprise.model.Message message = (com.gogowan.agroenterprise.model.Message) Utils
                            .fromJSON((String) res, com.gogowan.agroenterprise.model.Message.class);
                    String string = Utils.getMessageContent(message);

                    if (string == null || string.equals("")) {
                        showToast(getString(R.string.analyse_error));
                    } else {
                        ResultInfo resultInfo = (ResultInfo) Utils.fromJSON(string, ResultInfo.class);
                        if (resultInfo.getResult() == 32000) {
                            // 成功
                            showToast(resultInfo.getResultinfo());
                            for (CollectCode collectCode: adapater.isCheckItem()){
                                dbHelper.deleteBydata(collectCode.getCollectTime());
                            }
//                            finish();
                            removeState();
                        } else if (resultInfo.getResult() == -32000) {
                            // -32000返回值不用缓存数据
                            showToast(resultInfo.getResultinfo());
                        } else {
                            // 其他异常缓存数据
                            showToast(resultInfo.getResultinfo());
                        }
                    }
                }
            } catch (Exception ex) {
                showToast(getString(R.string.analyse_error));
            }
            super.onPostExecute(res);
        }
    }
    private void removeState(){
        strings=dbHelper.getHistroyList();
        adapater=new NewCollectHistoryAdapater(this,strings);
        adapater.setListener(listener);
        listView.setAdapter(adapater);
    }
}
